﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class AdminDAL
    {
        public static int Save(AdminInfo info)
        {
            string sql = string.Format("insert into [admin]([UserName],[UserPwd],[RealName],[ComName],[Mobile],[QQ],[WeiXin],[Power],[Menu1],[Menu2],[Menu3],[Menu4],[Menu5],[Menu6],[Menu7],[Menu8],[News],[Quan],[Active],[Copyright1],[Copyright2],[Copyright3],[Copyright4],[Copyright5],[Hot],[Score],[AppId],[AppSecret],[MchId],[AppKey],[CreateDate]) values(@UserName,@UserPwd,@RealName,@ComName,@Mobile,@QQ,@WeiXin,@Power,@Menu1,@Menu2,@Menu3,@Menu4,@Menu5,@Menu6,@Menu7,@Menu8,@News,@Quan,@Active,@Copyright1,@Copyright2,@Copyright3,@Copyright4,@Copyright5,@Hot,@Score,@AppId,@AppSecret,@MchId,@AppKey,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@ComName", info.ComName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@QQ", info.QQ),
                new SqlParameter("@WeiXin", info.WeiXin),
                new SqlParameter("@Power", "menu-1,all-1,menu-10,btns-10-1,btns-10-2,btns-10-3,menu-27,btns-27-1,btns-27-2,btns-27-3,menu-23,all-23,menu-3,btns-3-1,btns-3-2,btns-3-3,menu-24,all-24,menu-11,btns-11-1,btns-11-2,btns-11-3,menu-13,btns-13-1,btns-13-2,btns-13-3,btns-13-6,menu-20,btns-20-1,btns-20-2,btns-20-3,menu-29,btns-29-1,btns-29-2,btns-29-3,menu-30,btns-30-2,btns-30-3,btns-30-7,menu-25,all-25,menu-14,btns-14-1,btns-14-2,btns-14-3,menu-31,all-31,menu-32,btns-32-1,btns-32-2,btns-32-3,menu-5,all-5,menu-21,menu-28,menu-18,menu-33"),
                new SqlParameter("@Menu1", "1&1&新品&/img/new.png&/pages/product/product?order=createdate"),
                new SqlParameter("@Menu2", "2&1&热卖&/img/hot.png&/pages/product/product?order=sellcount"),
                new SqlParameter("@Menu3", "3&1&促销&/img/cuxiao.png&/pages/product/product?order=aprice"),
                new SqlParameter("@Menu4", "4&1&全部&/img/all.png&/pages/product/product?order=allrank"),
                new SqlParameter("@Menu5", "0&0&&&"),
                new SqlParameter("@Menu6", "0&0&&&"),
                new SqlParameter("@Menu7", "0&0&&&"),
                new SqlParameter("@Menu8", "0&0&&&"),
                new SqlParameter("@News", "1&1&4&商城公告"),
                new SqlParameter("@Quan", "2&1&4&优惠券"),
                new SqlParameter("@Active", "3&1&4&活动专区"),
                new SqlParameter("@Copyright1", "互联网一站式解决方案"),
                new SqlParameter("@Copyright2", "Copyright&nbsp;icoi&nbsp;2014-2018"),
                new SqlParameter("@Copyright3", "/image/logo.png"),
                new SqlParameter("@Copyright4", "铭恩科技提供技术支持&nbsp;&nbsp;"),
                new SqlParameter("@Copyright5", "0371-60905321"),
                new SqlParameter("@Hot", ""),
                new SqlParameter("@Score", 1),
                new SqlParameter("@AppId", ""),
                new SqlParameter("@AppSecret", ""),
                new SqlParameter("@MchId", ""),
                new SqlParameter("@AppKey", ""),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [UserName]=@UserName,[UserPwd]=@UserPwd,[RealName]=@RealName where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }

        public static int UpdateCopyright(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [Copyright1]=@Copyright1,[Copyright2]=@Copyright2,[Copyright3]=@Copyright3,[Copyright4]=@Copyright4,[Copyright5]=@Copyright5 where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Copyright1", info.Copyright1),
                new SqlParameter("@Copyright2", info.Copyright2),
                new SqlParameter("@Copyright3", info.Copyright3),
                new SqlParameter("@Copyright4", info.Copyright4),
                new SqlParameter("@Copyright5", info.Copyright5),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int UpdateConfig(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [Score]=@Score,[AppId]=@AppId,[AppSecret]=@AppSecret,[MchId]=@MchId,[AppKey]=@AppKey where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Score", info.Score),
                new SqlParameter("@AppId", info.AppId),
                new SqlParameter("@AppSecret", info.AppSecret),
                new SqlParameter("@MchId", info.MchId),
                new SqlParameter("@AppKey", info.AppKey),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static AdminInfo FindOneByID(int id)
        {
            DataRow row = CommonDAL.FindOneByID(id, "admin");
            return SimpleData(row);
        }


        public static AdminInfo Login(string username, string userpwd)
        {
            DataRow row = CommonDAL.GetDataRow("select * from admin where username='" + username + "' and userpwd='" + userpwd + "'");
            return SimpleData(row);
        }
        
        public static List<AdminInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [admin].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [admin].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[admin]", out count);
            return ListSimpleData(dt);
        }
        public static List<AdminInfo> FindListByRole(int role)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [admin] where Role={0}",role));
            return ListSimpleData(dt);
        }
        private static AdminInfo SimpleData(DataRow dr)
        {
            AdminInfo info = new AdminInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DataConvert.ToString(dr["UserPwd"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.ComName = DataConvert.ToString(dr["ComName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.QQ = DataConvert.ToString(dr["QQ"]);
                info.WeiXin = DataConvert.ToString(dr["WeiXin"]);
                info.Power = DataConvert.ToString(dr["Power"]);
                info.Menu1 = DataConvert.ToString(dr["Menu1"]);
                info.Menu2 = DataConvert.ToString(dr["Menu2"]);
                info.Menu3 = DataConvert.ToString(dr["Menu3"]);
                info.Menu4 = DataConvert.ToString(dr["Menu4"]);
                info.Menu5 = DataConvert.ToString(dr["Menu5"]);
                info.Menu6 = DataConvert.ToString(dr["Menu6"]);
                info.Menu7 = DataConvert.ToString(dr["Menu7"]);
                info.Menu8 = DataConvert.ToString(dr["Menu8"]);
                info.News = DataConvert.ToString(dr["News"]);
                info.Quan = DataConvert.ToString(dr["Quan"]);
                info.Active = DataConvert.ToString(dr["Active"]);
                info.Hot = DataConvert.ToString(dr["Hot"]);
                info.Score = DataConvert.ToDouble(dr["Score"]);
                info.AppId = DataConvert.ToString(dr["AppId"]);
                info.AppSecret = DataConvert.ToString(dr["AppSecret"]);
                info.MchId = DataConvert.ToString(dr["MchId"]);
                info.AppKey = DataConvert.ToString(dr["AppKey"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.Copyright1 = DataConvert.ToString(dr["Copyright1"]).Replace("icoi", "©");
                info.Copyright2 = DataConvert.ToString(dr["Copyright2"]).Replace("icoi", "©");
                info.Copyright3 = DataConvert.ToString(dr["Copyright3"]).Replace("icoi", "©");
                info.Copyright4 = DataConvert.ToString(dr["Copyright4"]).Replace("icoi", "©");
                info.Copyright5 = DataConvert.ToString(dr["Copyright5"]).Replace("icoi", "©");
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Token = DESEncrypt.Encrypt(info.ID.ToString());
            }
            return info;
        }
        private static AdminInfo AllData(DataRow dr)
        {
            AdminInfo info = new AdminInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DataConvert.ToString(dr["UserPwd"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.ComName = DataConvert.ToString(dr["ComName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.QQ = DataConvert.ToString(dr["QQ"]);
                info.WeiXin = DataConvert.ToString(dr["WeiXin"]);
                info.Power = DataConvert.ToString(dr["Power"]);
                info.Menu1 = DataConvert.ToString(dr["Menu1"]);
                info.Menu2 = DataConvert.ToString(dr["Menu2"]);
                info.Menu3 = DataConvert.ToString(dr["Menu3"]);
                info.Menu4 = DataConvert.ToString(dr["Menu4"]);
                info.Menu5 = DataConvert.ToString(dr["Menu5"]);
                info.Menu6 = DataConvert.ToString(dr["Menu6"]);
                info.Menu7 = DataConvert.ToString(dr["Menu7"]);
                info.Menu8 = DataConvert.ToString(dr["Menu8"]);
                info.News = DataConvert.ToString(dr["News"]);
                info.Quan = DataConvert.ToString(dr["Quan"]);
                info.Active = DataConvert.ToString(dr["Active"]);
                info.Hot = DataConvert.ToString(dr["Hot"]);
                info.Score = DataConvert.ToDouble(dr["Score"]);
                info.AppId = DataConvert.ToString(dr["AppId"]);
                info.AppSecret = DataConvert.ToString(dr["AppSecret"]);
                info.MchId = DataConvert.ToString(dr["MchId"]);
                info.AppKey = DataConvert.ToString(dr["AppKey"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.Copyright1 = DataConvert.ToString(dr["Copyright1"]).Replace("icoi", "©");
                info.Copyright2 = DataConvert.ToString(dr["Copyright2"]).Replace("icoi", "©");
                info.Copyright3 = DataConvert.ToString(dr["Copyright3"]).Replace("icoi", "©");
                info.Copyright4 = DataConvert.ToString(dr["Copyright4"]).Replace("icoi", "©");
                info.Copyright5 = DataConvert.ToString(dr["Copyright5"]).Replace("icoi", "©");
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Token = DESEncrypt.Encrypt(info.ID.ToString());
            }
            return info;
        }
        private static List<AdminInfo> ListSimpleData(DataTable dt)
        {
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<AdminInfo> ListAllData(DataTable dt)
        {
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }

    }
}
